package com.zhang.redis.common.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-11-30 13:58
 **/
@Slf4j
@RequiredArgsConstructor
@Component
public class RedisValueOpsManager<T> {

    @Resource
    private  RedisTemplate<String, T> redisTemplate;

    public RedisTemplate<String,T> getRedisTemplate(){
        return redisTemplate;
    }

    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    public void set(String key,T value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void set(String key,T value, long timeout, TimeUnit unit){
        redisTemplate.opsForValue().set(key,value,timeout,unit);
    }

    public void set(String key,T value, long timeout){
        redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.MILLISECONDS);
    }

    public T get(String key){
        return  redisTemplate.opsForValue().get(key);
    }

    public List<T> multiGet(List<String> keys){
        return  redisTemplate.opsForValue().multiGet(keys);
    }

    public Long increment(String key){
        return  redisTemplate.opsForValue().increment(key);
    }

    public Long decrement(String key){
        return  redisTemplate.opsForValue().decrement(key);
    }

    public Long size(String key){
        return  redisTemplate.opsForValue().size(key);
    }

    public Boolean setIfPresent(String key, T value, long timeout, TimeUnit unit){
        return  redisTemplate.opsForValue().setIfPresent(key,value,timeout,unit);
    }

    public Boolean setIfPresent(String key, T value, long timeout){
        return  redisTemplate.opsForValue().setIfPresent(key,value,timeout,TimeUnit.MILLISECONDS);
    }
}
